package org.dromara.admin.service.impl;

import org.dromara.admin.domain.vo.EmployeeVo;
import org.dromara.admin.service.IEmployeeService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.admin.domain.bo.SystemConfigBo;
import org.dromara.admin.domain.vo.SystemConfigVo;
import org.dromara.admin.domain.SystemConfig;
import org.dromara.admin.mapper.SystemConfigMapper;
import org.dromara.admin.service.ISystemConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 系统配置Service业务层处理
 *
 * @author Lion Li
 * @date 2025-10-10
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SystemConfigServiceImpl implements ISystemConfigService {

    private final SystemConfigMapper baseMapper;
    private final IEmployeeService employeeService;

    /**
     * 查询系统配置
     *
     * @param id 主键
     * @return 系统配置
     */
    @Override
    public SystemConfigVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 根据员工id查询系统配置
     *
     * @param id 员工id
     * @return 系统配置
     */
    @Override
    public SystemConfigVo queryConfigInfoByEmployeeId(Long id) {
        EmployeeVo employeeVo = employeeService.queryById(id);
        return baseMapper.selectVoOne(new LambdaQueryWrapper<SystemConfig>()
            .eq(SystemConfig::getCompanyId, employeeVo.getCompanyId()));
    }

    /**
     * 分页查询系统配置列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 系统配置分页列表
     */
    @Override
    public TableDataInfo<SystemConfigVo> queryPageList(SystemConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SystemConfig> lqw = buildQueryWrapper(bo);
        Page<SystemConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的系统配置列表
     *
     * @param bo 查询条件
     * @return 系统配置列表
     */
    @Override
    public List<SystemConfigVo> queryList(SystemConfigBo bo) {
        LambdaQueryWrapper<SystemConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SystemConfig> buildQueryWrapper(SystemConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SystemConfig> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(SystemConfig::getId);
        lqw.eq(bo.getCompanyId() != null, SystemConfig::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getStockStat() != null, SystemConfig::getStockStat, bo.getStockStat());
        lqw.eq(bo.getCustomerPrice() != null, SystemConfig::getCustomerPrice, bo.getCustomerPrice());
        lqw.eq(bo.getSettleMode() != null, SystemConfig::getSettleMode, bo.getSettleMode());
        lqw.eq(bo.getTagMode() != null, SystemConfig::getTagMode, bo.getTagMode());
        lqw.eq(bo.getLogisticsMode() != null, SystemConfig::getLogisticsMode, bo.getLogisticsMode());
        lqw.eq(bo.getFinanceMode() != null, SystemConfig::getFinanceMode, bo.getFinanceMode());
        lqw.eq(bo.getMultiAddress() != null, SystemConfig::getMultiAddress, bo.getMultiAddress());
        lqw.eq(bo.getTwoLevelCategory() != null, SystemConfig::getTwoLevelCategory, bo.getTwoLevelCategory());
        return lqw;
    }

    /**
     * 新增系统配置
     *
     * @param bo 系统配置
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SystemConfigBo bo) {
        SystemConfig add = MapstructUtils.convert(bo, SystemConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 根据主体id新增系统配置
     *
     * @param companyId 主体公司id
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByCompanyId(Long companyId) {
        SystemConfigBo systemConfigBo = new SystemConfigBo();
        systemConfigBo.setCompanyId(companyId);
        return insertByBo(systemConfigBo);
    }

    /**
     * 修改系统配置
     *
     * @param bo 系统配置
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SystemConfigBo bo) {
        SystemConfig update = MapstructUtils.convert(bo, SystemConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SystemConfig entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除系统配置信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
